package com.lzw.controller.superAdmin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.Medicines;
import com.lzw.pojo.StockEntries;
import com.lzw.service.superAdmin.MedicinesManageService;
import com.lzw.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/MedicinesManege")
public class MedicinesManage {

    @Autowired
    private MedicinesManageService medicinesManageService;

    @Autowired
    private OSSUtil ossUtil;

    @GetMapping("/list")
    public PageInfo<Medicines> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name, String batchNumber, String manufacturer, String supplier) {
        PageHelper.startPage(pageNum, pageSize);
        List<Medicines> list = medicinesManageService.list(pageNum, pageSize, name, batchNumber, manufacturer, supplier);
        return new PageInfo<>(list);
    }

    //批量删除
    @Log
    @DeleteMapping("/delete/{ids}")
    public String delete(@PathVariable List<Integer> ids) {
        medicinesManageService.delete(ids);
        return "删除成功";
    }

    //添加
    @Log
    @PostMapping("/add")
    public String add(@RequestParam("name") String name,
                      @RequestParam("batchNumber") String batchNumber,
                      @RequestParam("price") String price,
                      @RequestParam("quantity") Integer quantity,
                      @RequestParam("manufacturer") String manufacturer,
                      @RequestParam("supplier") String supplier,
                      @RequestParam("saleQuantity") Integer saleQuantity,
                      @RequestParam("image") MultipartFile imageFile) {
        try {
            String imageUrl = ossUtil.uploadImage(imageFile, "medicines_image");

            //创建药品对象
            Medicines medicines = new Medicines();
            medicines.setName(name);
            medicines.setBatchNumber(batchNumber);
            medicines.setPrice(price);
            medicines.setQuantity(quantity);
            medicines.setManufacturer(manufacturer);
            medicines.setSupplier(supplier);
            medicines.setSaleQuantity(saleQuantity);
            medicines.setImage(imageUrl);
            medicinesManageService.add(medicines);
            return "添加成功";
        } catch (Exception e) {
            return "添加失败";
        }
    }

    //更新
    @Log
    @PutMapping("/update")
    public String update(@RequestParam("medicineId") Integer medicineId,
                         @RequestParam(value = "name", required = false) String name,
                         @RequestParam(value = "batchNumber", required = false) String batchNumber,
                         @RequestParam(value = "price", required = false) String price,
                         @RequestParam(value = "quantity", required = false) Integer quantity,
                         @RequestParam(value = "manufacturer", required = false) String manufacturer,
                         @RequestParam(value = "supplier", required = false) String supplier,
                         @RequestParam(value = "saleQuantity", required = false) Integer saleQuantity,
                         @RequestParam(value = "image", required = false) MultipartFile imageFile) {

        try {

            //查找
            Medicines medicines = medicinesManageService.getById(medicineId);

            if (medicines == null) {
                return "不存在";
            }

            //创建药品对象
            if (name != null) medicines.setName(name);
            if (batchNumber != null) medicines.setBatchNumber(batchNumber);
            if (price != null) medicines.setPrice(price);
            if (quantity != null) medicines.setQuantity(quantity);
            if (manufacturer != null) medicines.setManufacturer(manufacturer);
            if (supplier != null) medicines.setSupplier(supplier);
            if (saleQuantity != null) medicines.setSaleQuantity(saleQuantity);

            if (imageFile != null && !imageFile.isEmpty()) {
                String imageUrl = ossUtil.uploadImage(imageFile, "medicines_image");
                medicines.setImage(imageUrl);
            }
            medicinesManageService.update(medicines);
            return "更新成功";
        } catch (Exception e) {
            return "更新失败";
        }
    }
}
